package com.cm.server.cmone.controller;

import com.cm.server.cmone.entity.MenuItem;
import com.cm.server.cmone.repository.MenuItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuItemRepository menuItemRepository;

    @GetMapping
    public List<MenuItem> getMenuItems() {
        // 如果数据库中有数据，则从数据库获取
        List<MenuItem> menuItems = menuItemRepository.findAll();
        if (!menuItems.isEmpty()) {
            return menuItems;
        }
        
        // 如果数据库中没有数据，则返回示例数据
        return getSampleMenuItems();
    }
    
    @PostMapping
    public MenuItem createMenuItem(@RequestBody MenuItem menuItem) {
        return menuItemRepository.save(menuItem);
    }
    
    @PutMapping("/{id}")
    public MenuItem updateMenuItem(@PathVariable Long id, @RequestBody MenuItem menuItem) {
        menuItem.setId(id);
        return menuItemRepository.save(menuItem);
    }
    
    @DeleteMapping("/{id}")
    public void deleteMenuItem(@PathVariable Long id) {
        menuItemRepository.deleteById(id);
    }
    
    @GetMapping("/key/{key}")
    public List<MenuItem> getMenuItemsByKey(@PathVariable String key) {
        return menuItemRepository.findByKey(key);
    }
    
    @PostMapping("/init")
    public List<MenuItem> initMenuItems() {
        // 清空现有数据
        menuItemRepository.deleteAll();
        
        // 创建示例数据并保存到数据库
        List<MenuItem> sampleMenuItems = getSampleMenuItems();
        return menuItemRepository.saveAll(sampleMenuItems);
    }

    private List<MenuItem> getSampleMenuItems() {
        // 创建叶子节点
        MenuItem bookmark1 = new MenuItem("bookmark-1", "收藏夹 1");
        MenuItem bookmark2 = new MenuItem("bookmark-2", "收藏夹 2");
        MenuItem bookmark3 = new MenuItem("bookmark-3", "收藏夹 3");
        
        MenuItem favorites1 = new MenuItem("favorites-1", "我的收藏");
        MenuItem favorites2 = new MenuItem("favorites-2", "最近访问");
        MenuItem favorites3 = new MenuItem("favorites-3", "热门推荐");
        
        MenuItem software1 = new MenuItem("software-1", "开发工具");
        MenuItem software2 = new MenuItem("software-2", "办公软件");
        MenuItem software3 = new MenuItem("software-3", "娱乐软件");
        
        MenuItem blogAll = new MenuItem("blog-all", "所有文章");
        MenuItem blogTech = new MenuItem("blog-tech", "技术文章");
        MenuItem blogLife = new MenuItem("blog-life", "生活随笔");
        
        // 创建带子菜单的节点
        MenuItem home = new MenuItem("home", "首页", "BookOutlined", Arrays.asList());
        MenuItem bookmark = new MenuItem("bookmark", "网页收藏", "BookOutlined", 
                Arrays.asList(bookmark1, bookmark2, bookmark3));
        MenuItem favorites = new MenuItem("favorites", "个人收藏", "StarOutlined", 
                Arrays.asList(favorites1, favorites2, favorites3));
        MenuItem software = new MenuItem("software", "常用软件", "AppstoreOutlined", 
                Arrays.asList(software1, software2, software3));
        MenuItem blog = new MenuItem("blog", "个人博客", "StarOutlined", 
                Arrays.asList(blogAll, blogTech, blogLife));
        
        // 返回完整的菜单列表
        return Arrays.asList(home, bookmark, favorites, software, blog);
    }
}